package com.dxyxx.framework;

import org.apache.http.Header;
import org.codehaus.jackson.type.JavaType;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import br.com.pierry.simpletoast.SimpleToast;
import cn.smssdk.EventHandler;
import cn.smssdk.SMSSDK;

import com.android.customized.CountdownButtonHandler;
import com.dxyxx.R;
import com.dxyxx.android.setting.UserPreferences;
import com.dxyxx.dialog.AgainPlayer;
import com.dxyxx.dialog.WaittingCircle;
import com.dxyxx.pojo.ResponseResult;
import com.dxyxx.pojo.User;
import com.http.HttpUtil;
import com.loopj.android.http.AsyncHttpResponseHandler;
import com.loopj.android.http.RequestParams;
import com.ta.utdid2.android.utils.StringUtils;

/**
 * @deprecated 作废
 * @author Administrator
 * 
 */
@Deprecated
public class MobLoginActivity extends BaseActivity {

	private EditText phone;
	private EditText code;
	private Context context;
	private CountdownButtonHandler handler;
	private EventHandler eh;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_smslogin);
		context = this;
		phone = (EditText) findViewById(R.id.phone);
		phone.setSelectAllOnFocus(true);
		phone.setFocusable(true);
		phone.setFocusableInTouchMode(true);

		code = (EditText) findViewById(R.id.code);
		code.setSelectAllOnFocus(true);
		code.setFocusable(true);
		code.setFocusableInTouchMode(true);

		//创建计数器
		initViews();
		handler = new CountdownButtonHandler(getApplicationContext(), codeButton);
		userPreferences = UserPreferences.getInstance(getApplicationContext());
		//init smssdk

		eh = new EventHandler() {

			@Override
			public void afterEvent(int event, int result, Object data) {

				if (result == SMSSDK.RESULT_COMPLETE) {
					//回调完成
					if (event == SMSSDK.EVENT_SUBMIT_VERIFICATION_CODE) {
						//提交验证码成功

					} else if (event == SMSSDK.EVENT_GET_VERIFICATION_CODE) {
						//获取验证码成功
						// 使用 handler。。 
						handler.sendEmptyMessage(0);
					} else if (event == SMSSDK.EVENT_GET_SUPPORTED_COUNTRIES) {
						//返回支持发送验证码的国家列表
					}
				} else {
					((Throwable) data).printStackTrace();
				}
				SMSSDK.unregisterEventHandler(this);
			}
		};

	}

	private UserPreferences userPreferences;
	private Button codeButton;
	private Button loginButton;
	private ProgressDialog progressDialog;//全局等待框

	private void initViews() {
		((ImageView) findViewById(R.id.backImageButton)).setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				finish();
			}
		});
		((ImageView) findViewById(R.id.nextImageButton)).setVisibility(View.INVISIBLE);
		TextView titleView = (TextView) findViewById(R.id.title);
		titleView.setText("手机验证码登陆");
		titleView.setGravity(Gravity.LEFT | Gravity.CENTER_VERTICAL);
		// 点击发送验证码。。等待30秒
		codeButton = (Button) findViewById(R.id.b_send);
		loginButton = (Button) findViewById(R.id.b_login);
		codeButton.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				// 判断手机号 是否填写正确
				String phoneStr = phone.getText().toString().trim();
				if (StringUtils.isEmpty(phoneStr)) {
					SimpleToast.error(context, "手机号不能为空");
					return;
				} else {
					SMSSDK.registerEventHandler(eh);
					//发送手机验证码
					SMSSDK.getVerificationCode("86", phoneStr.trim());
				}
			}
		});

		loginButton.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				// 判断手机号 是否填写正确
				String phoneStr = phone.getText().toString().trim();
				String codeStr = code.getText().toString().trim();
				if (StringUtils.isEmpty(phoneStr) || StringUtils.isEmpty(codeStr)) {
					SimpleToast.error(context, "手机号|验证码 不能为空");
					return;
				} else {
					// 发送http 请求
					String url = "mob/login";
					RequestParams rp = new RequestParams();
					rp.put("phone", phoneStr);
					rp.put("code", codeStr);
					// 禁止 登陆按钮点击了。
					loginButton.setClickable(false);
					// 启动等待框
					progressDialog = WaittingCircle.showDialog(context, false);

					HttpUtil.post(url, rp, new AsyncHttpResponseHandler() {

						@Override
						public void onSuccess(int arg0, Header[] arg1, byte[] arg2) {
							try {
								JavaType javaType = getObjectMapper().getTypeFactory().constructParametricType(ResponseResult.class, User.class);
								ResponseResult<User> rr = getObjectMapper().readValue(arg2, javaType);

								//{"object":null,"code":1,"info":"验证码已发送，稍等"}
								if (rr.getCode() == 1) { //成功
									// 显示信息
									SimpleToast.ok(context, "登陆成功");
									// 存储 用户名密码、
									//									final User user = new User(rr.getObject().getPhone(), rr.getObject().getPassword(), System.currentTimeMillis(), true);
									//									userPreferences.setUser(user);
									//									userPreferences.setToken(rr.getInfo());

									// 关闭 activity，返回去
									Intent intent = new Intent();
									// 把返回数据存入Intent
									intent.putExtra("result", true);
									// 设置返回数据
									MobLoginActivity.this.setResult(ResultCode.MobLogin, intent);
									// 关闭Activity
									MobLoginActivity.this.finish();
								} else { //失败 
									// 显示信息
									SimpleToast.info(context, rr.getInfo());
									loginButton.setClickable(true);//激活 按钮
								}
							} catch (Exception e) {
								e.printStackTrace();
								SimpleToast.error(context, "系统内部出错！");
								loginButton.setClickable(true);//激活 按钮
							}
							progressDialog.dismiss();
						}

						@Override
						public void onFailure(int arg0, Header[] arg1, byte[] arg2, Throwable arg3) {
							AgainPlayer.makeCall((Activity) context, "网络好像出错了");
							loginButton.setClickable(true);//激活 按钮
							progressDialog.dismiss();
						}
					});
				}

			}
		});
	}
}
